<?php
////////////////////////////////////////////////////////////////////////////////
//	  This file is part of PushLMS.
//
//    PushLMS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    PushLMS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with PushLMS.  If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

class CoursesController extends AppController {

	var $scaffold = 'admin';
	var $helpers = array('Form', 'Javascript', 'Ajax');
	var $uses = array('Course', 'User');

	function beforeFilter() {

	}

	################
	# C.R.U.D METHODS
	################

	function index(){

		$this->set("CSS", "main");
		$user = $this->User->findById($this->Session->read('Auth.User.id'));
		$this->set('user', $user);

	}

	function search(){

		$courses = $this->Course->find('all');
		$this->set('courses', $courses);

	}

	function add() {
			

		if (!empty($this->data)) {

			if ($this->Course->save($this->data)){

				$this->Session->setFlash("New Course Added!");
				$this->redirect('/courses/search');

			}

		}

	}

	function edit($id = null) {


		if (!empty($this->data)) {

			if ($this->Course->save($this->data)){
			
				$this->Session->setFlash("Course Updated!");
				$this->redirect('/courses/search');
			}

		}

		$course = $this->Course->findById($id);

		$this->set('course', $course);
		$this->data = $course;

	}

	function delete($id=null){


		if ($id != null){
			$this->Course->delete($id);
			$this->Session->setFlash("Course Removed!");
			$this->redirect('/courses/search/');
		}

	}

	function duplicate($id=null){

		Configure::write('debug', 0);

		$course = $this->Course->findById($id);

		if (!empty($id)) {
			
			$this->Course->copy($id);
			$this->Session->setFlash('Course Duplicated');

		}

		$this->redirect('/courses/search/');
		
	}

}

?>
